﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Windows;
using System.Windows.Controls;
using System.Linq;
using System.Text;
using System.Xml;
using System.Diagnostics;
using System.Windows.Data;
using System.IO;
using DoubleH.Plugins;
using Table=FCNS.Data.Table;
using DoubleH.Utility;
using System.Windows.Controls.Primitives;
using DoubleH.Utility.Configuration;
using DoubleH.Utility.IO;
using System.Collections;

namespace DoubleH
{
    public partial class WorkArea : Window
    {
        private void TreeViewNodeClick()
        {
            //alert 要防止重复点击相同的  重复读取数据库
            if (selectedMenu == null)
                return;

            ConfigDataGridTop();
            dataGridObject.InitColumns(selectedMenu.DataGridColumn);
            BeginBindData();
        }

        private void ConfigDataGridTop()
        {
            if (selectedMenu == null)
            {
                buttonNew.IsEnabled = false;
                expanderMore.IsEnabled = false;
                return;
            }
            expanderMore.IsEnabled = true;

            button_print.IsEnabled = selectedMenu.CanPrint;
            //button_import.IsEnabled = selectedMenu.CanImport;
            buttonNew.IsEnabled = selectedMenu.CanNew;
            if (selectedMenu.NewButtonText != string.Empty)
                buttonNew.Content = selectedMenu.NewButtonText;
            else
                buttonNew.Content = "创建";


            //分组数据
            if (DoubleHConfig.UserConfig.GroupData)
            {
                canvasGroup.Visibility = Visibility.Visible;
                labelIndex1.Content = "00001";
                labelIndex2.Content = DoubleHConfig.UserConfig.GroupDataCount.ToString("D5");
            }
            else
                canvasGroup.Visibility = Visibility.Collapsed;
        }

        //private void EventImport()
        //{
        //    FCNS.Data.Interface.DataIO io= null;
        //    switch (selectedMenu.TableText)
        //    {
        //        case DataTableText.客户:
        //            break;
        //        default:
        //            MessageWindow.Show("当前类别不支持数据导入");
        //            break;
        //    }
        //    if (io != null)
        //        io.Import(string.Empty);
        //}

        private void EventExport()
        {
            ExportWindow ew = new ExportWindow(dataGridObject);
            ew.Show();
        }

        private void EventDelete()
        {
            if (selectedMenu.Authority < Table.UserS.EnumAuthority.删除)
            {
                MessageWindow.Show("权限不足");
                return;
            }

            if (MessageWindow.Show("", "确定要删除选中项目吗?", MessageBoxButton.YesNo) == MessageBoxResult.No)
                return;

            if (selectedMenu.TableText == DataTableText.Pos禁售商品)
            {
                Table.ProductS ps = dataGridObject.SelectedItem as Table.ProductS;
                if (ps != null)
                    ps.SetPosCanNotUseList(false);
            }
            else
            {
                int i = 0;
                List<object> del = new List<object>();
                foreach (object obj in dataGridObject.SelectedItems)
                {
                    FCNS.Data.Interface.IDataTable table = obj as FCNS.Data.Interface.IDataTable;
                    if (table != null)
                        if (table.Delete() == Table.DataTableS.EnumDatabaseStatus.操作成功)
                        {
                            del.Add(obj);
                            continue;
                        }
                    i++;
                }
                IList all = (IList)dataGridObject.ItemsSource;
                del.ForEach(f => all.Remove(f));
                if (i != 0)
                    MessageWindow.Show(i.ToString() + " 张单据存在业务来往,无法删除");
            }
            //BeginBindData();
        }

        private void EventZuoFei()
        {
            //if (dataGridObject.SelectedItem == null)
            //{
            //    MessageWindow.Show("请选择要停用/作废的项目");
            //    return;
            //}
            if (MessageWindow.Show("", "确定要停用/作废选中项目吗?", MessageBoxButton.YesNo) == MessageBoxResult.No)
                return;

            if (selectedMenu.TableText == DataTableText.Pos禁售商品)
            {
                Table.ProductS ps = dataGridObject.SelectedItem as Table.ProductS;
                if (ps != null)
                    ps.SetPosCanNotUseList(false);
            }
            else
            {
                int i = 0;
                foreach (object obj in dataGridObject.SelectedItems)
                {
                    FCNS.Data.Interface.IDataTable table = obj as FCNS.Data.Interface.IDataTable;
                    if (table != null)
                        if (table.ZuoFei() == Table.DataTableS.EnumDatabaseStatus.操作成功)
                            continue;

                    i++;
                }
                if (i != 0)
                    MessageWindow.Show(i.ToString() + " 张单据存在业务来往,无法删除");
            }
            //BeginBindData();
        }

        private void LoginOff()
        {
            ConfigSerializer.SaveConfig(DoubleHConfig.UserConfig, Table.UserS.LoginUser.Id);
            this.Hide();
            if (!NormalUtility.ShowLoginWindowSuccess())
            {
                Application.Current.Shutdown();
                return;
            }
            else
            {
                InitUserVar();
                this.Show();
            }
        }

        private void OpenHelp()
        {
            AboutMe am = new AboutMe();
            am.ShowDialog();
        }

        private void OpenCalc()
        {
            System.Diagnostics.Process.Start("calc.exe");
        }
        /// <summary>
        /// 如果系统日期被更改了，就自动注销系统。
        /// </summary>
        private void CheckDateIsChanged()
        {
            Microsoft.Win32.SystemEvents.TimeChanged += (ss, ee) =>
                {
                    if (DateTime.Now.Date != Table.SysConfig.SysConfigParams.LastLoginDate.Date)
                        LoginOff();
                };
        }

        ///// <summary>
        ///// 刷新 DataGrid 的数据
        ///// </summary>
        //public void RefreshDataGrid()
        //{
        //    if (DoubleHConfig.UserConfig.SynchronousData && !clickSearch)
        //        BeginBindData();
        //}

        #region CPU event

        void CPU_ShenHeTableObject(FCNS.Data.Interface.IDataTable table)
        {
            switch (table.TableName)
            {
                case "WuLiuS":
                    WuLiu(table as Table.StoreOrderS);
                    break;

            }
        }

        void CPU_InsertTableObject(FCNS.Data.Interface.IDataTable table)
        {
            if (table.TableName == selectedMenu.TableName&&!dataGridObject.Items.Contains(table))
                ((IList)dataGridObject.ItemsSource).Add(table);
        }

        void CPU_ReloadTableList(FCNS.Data.Interface.IDataTable table)
        {
            if (table.TableName == "AfterSaleServiceS")
                ((IList)dataGridObject.ItemsSource).Remove(table);
        }

        private void WuLiu(Table.StoreOrderS order)
        {
            if (Table.SysConfig.SysConfigParams.UseWuLiuS && Table.UserS.LoginUser.GetAuthority(DataTableText.物流跟踪)>Table.UserS.EnumAuthority.查看)
            {
                if (order.Flag == Table.StoreOrderS.EnumFlag.出入库单 && order.ChuKu)
                    pluginManager.LoadPlugin("WuLiuS.dll", null,  DataTableText.物流跟踪,null, order.OrderNO );
            }
        }
        #endregion
    }
}
